﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Business.SystemManage;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Service.RedBookSetManage;
using YiSha.Entity.RedBookSetManage;
using YiSha.Service.OrganizationManage;
using YiSha.Entity.OrganizationManage;
using YiSha.Business.RedBookSetManage;

namespace YiSha.Business.AutoJob
{
    public class ExpandConsumeJob : IJobTask
    {
        public async Task<TData> Start()
        {

            TData obj = new TData();
            RedBookSetBll redBookSetBll = new RedBookSetBll();
            List<RedBookSetEntity> redBookSetList = await redBookSetBll.GetJobList();
            if (redBookSetList != null && redBookSetList.Count > 0)
            {
                foreach (RedBookSetEntity redBookSetEntity in redBookSetList)
                {
                    //判断是否符合条件
                    bool flagTask = true;
                    string statusText = "";
                    //超出今日预算自动等待
                    List<SysConsumeEntity> sysConsumeEntities = await new SysConsumeService().GetTodayConsumeBySetIdList(redBookSetEntity.Id);
                    var todayConsume = sysConsumeEntities.Sum(t => t.Consume);
                    if (todayConsume > redBookSetEntity.Budget)
                    {
                        flagTask = false;
                        statusText = "超出当日预算，等待执行";
                    }
                    //var startTime= DateTime.ParseExact(DateTime.Now.ToString("yyyyMMdd") +" "+ redBookSetEntity.StartTime,"yyyyMMdd HH:mm:ss" ,System.Globalization.CultureInfo.CurrentCulture);
                    //var endTime= DateTime.ParseExact(DateTime.Now.ToString("yyyyMMdd") + " "+redBookSetEntity.EndTime, "yyyyMMdd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
                    DateTime startTime = Convert.ToDateTime(redBookSetEntity.StartTime);
                    DateTime endTime = Convert.ToDateTime(redBookSetEntity.EndTime);
                    DateTime dateTime = DateTime.Now;
                    if (!(DateTime.Compare(startTime, dateTime) <= 0 && DateTime.Compare(endTime, dateTime) >= 0))
                    {
                       
                            flagTask = false;
                            statusText = "非投放时间，等待执行";
                    }
                    //判断账户余额
                    UserEntity userEntity = await new UserService().GetEntity((long)redBookSetEntity.UserId);
                    if (userEntity != null && userEntity.Money < 20)
                    {
                        flagTask = false;
                        statusText = "余额不足暂停";
                    }
                    if (!flagTask)
                    {
                        redBookSetEntity.StatusText = statusText;
                        redBookSetEntity.SetSwitch = statusText.Contains("暂停") ? (byte?)0 : (byte?)1;   //暂停
                        await new RedBookSetService().UpdateRedBookSet(redBookSetEntity);

                        continue;
                    }

                    //开始执行
                    SysConsumeEntity sysConsumeEntity = new SysConsumeEntity();
                    sysConsumeEntity.RedBookSetId = redBookSetEntity.Id;
                    sysConsumeEntity.UserId = redBookSetEntity.UserId;
                    var random = new Random();
                    sysConsumeEntity.ShowView = random.Next(280, 400);
                    sysConsumeEntity.ShowClick = random.Next(10, 20);
                    sysConsumeEntity.Consume = random.Next(12, 20);
                    sysConsumeEntity.Remaining = userEntity.Money - sysConsumeEntity.Consume;
                    //更新账户金额
                    userEntity.Money = sysConsumeEntity.Remaining;
                    //执行
                    redBookSetEntity.StatusText = "正在执行";
                    await new SysConsumeService().SaveFormTrans(sysConsumeEntity, userEntity, redBookSetEntity);

                }
            }
            obj.Tag = 1;
            obj.Message = "任务执行中";
            return obj;
        }

    }
}
